package hottargui.config;

import hottargui.framework.Game;
import hottargui.framework.IllegalActionException;
import hottargui.framework.PlayerColor;
import hottargui.framework.Position;
import hottargui.framework.State;
import hottargui.framework.TarguiState;
import hottargui.framework.TileType;
import hottargui.standard.StandardTile;

public class AlphaMoveState implements TarguiState {

	Game gameContext;

	public AlphaMoveState(Game game) {
		gameContext = game;
	}

	public boolean buy(int count, Position deploy)
			throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public State getState() {
		return State.move;
	}

	public boolean move(Position from, Position to, int count)
			throws IllegalActionException {
		StandardTile fromTile = (StandardTile) gameContext.getTile(from);
		StandardTile toTile = (StandardTile) gameContext.getTile(to);

		// check if we move more than 1 position vertically
		if (Math.abs(from.getRow() - to.getRow()) > 1) {
			return false;
		}
		// check if we move more than 1 position horizontally
		if (Math.abs(from.getColumn() - to.getColumn()) > 1) {
			return false;
		}
		// cannot move to saltlake
		if (toTile.getType() == TileType.Saltlake) {
			return false;
		}
		// cannot move if no camels is located on the tile
		if (fromTile.getUnitCount() == 0) {
			return false;
		}

		// decide if we should capture a tile or just go to our own tile
		if (toTile.getOwnerColor() != fromTile.getOwnerColor()) {

			// Is there any camels on destination tile, then we will go to war
			if (toTile.getUnitCount() > 0) {
				int fromCasualties = Math.max(0, fromTile.getUnitCount()
						- toTile.getUnitCount());
				int toCasualties = Math.max(0, toTile.getUnitCount()
						- fromTile.getUnitCount());
				fromTile.changeUnitCount(fromCasualties);
				toTile.changeUnitCount(toCasualties);
			}
			// Move to tile if there are any camels left
			if (fromTile.getUnitCount() > 0) {
				toTile.changePlayerColor(gameContext.getPlayerInTurn()
						.getColor());
			}
		}
		// Decide how many camels we can move
		if (fromTile.getUnitCount() < count) {
			toTile.changeUnitCount(toTile.getUnitCount()
					+ fromTile.getUnitCount());
			fromTile.changeUnitCount(0);
		} else {
			toTile.changeUnitCount(toTile.getUnitCount() + count);
			fromTile.changeUnitCount(fromTile.getUnitCount() - count);
		}
		gameContext.setState(gameContext.getFactory().getBuyState(gameContext));
		return true;
	}

	public void newGame() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor turnCard() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}
}
